package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import org.piaohao.model.Employee;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 17:23:24
 *
 * @author piaohao
 */
public class EmpController extends Controller {

    public static final String moduleName = "安全职员模块";

    public void list() {
        try {
            String orgId = getPara("orgId");
            String orgTreeId = getPara("orgTreeId");
            String type = getPara("type");
            Integer start = getParaToInt("start");
            Integer limit = getParaToInt("limit");
            List<Employee> emps;
            Long total;
            if (orgId != null && orgId.equals("root")) {
                emps = Employee.dao.find("select * from t_employee limit ?,?", start, limit);
                total = Db.queryLong("select count(*) from t_employee");
            } else {
                if (type.equals("机构")) {
                    orgId = Db.queryStr("select fk_org_id from t_org_org_tree where fk_org_tree_id=?", orgTreeId);
                    emps = Employee.dao.find("select * from t_employee where org_id=? limit ?,?", orgId, start, limit);
                    total = Db.queryLong("select count(*) from t_employee where org_id=?", orgId);
                } else if (type.equals("部门")) {
                    String depId = Db.queryStr("select fk_dep_id from t_dep_org_tree where fk_org_tree_id=?", orgTreeId);
                    emps = Employee.dao.find("select * from t_employee where dep_id=? limit ?,?", depId, start, limit);
                    total = Db.queryLong("select count(*) from t_employee where dep_id=?", depId);
                } else {
                    String posId = Db.queryStr("select fk_pos_id from t_pos_org_tree where fk_org_tree_id=?", orgTreeId);
                    emps = Employee.dao.find("select * from t_employee where pos_id=? limit ?,?", posId, start, limit);
                    total = Db.queryLong("select count(*) from t_employee where pos_id=?", posId);
                }
            }
            renderJson(JsonUtil.printGrid(total, emps));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getAll() {
        try {
            List<Employee> emps = Employee.dao.find("select * from t_employee");
            Long total = Db.queryLong("select count(*) from t_employee");
            renderJson(JsonUtil.printGrid(total, emps));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getByOrgId() {
        try {
            String orgId = getPara(0);
            List<Record> emps = Db.find("select e.*,o.name orgName from t_employee e left join t_organization o on e.org_id=o.id where o.id=?", orgId);
            Long total = Db.queryLong("select count(*) from t_employee where org_id=?", orgId);
            renderJson(JsonUtil.printGrid(total, emps));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void add() {
        try {
            Employee emp = getModel(Employee.class);
            if (emp.getStr("id") == null || emp.getStr("id").trim().equals("")) {
                emp.set("id", UUIDGenerator.getUUID());
                emp.save();
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Employee emp = getModel(Employee.class);
            emp.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String ids = getPara("ids");
            String[] idArr = ids.split(",");
            for (int i = 0; i < idArr.length; i++) {
                Employee.dao.deleteById(idArr[i]);
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Employee emp = Employee.dao.findById(id);
            setAttr("emp", emp);
            render("/pages/employee/detail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
